package com.party.admin.web.controller.analysis;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsUserVisit;
import com.party.core.service.analysis.IStatisticsUserVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: Project
 * @description: 用户分析控制器
 * @author: Huaqunhao
 * @create: 2018-08-08 16:12
 **/
@Controller
@RequestMapping("analysis/user")
public class StatisticsUserController {

    @Autowired
    private IStatisticsUserVisitService statisticsUserVisitService;



    /**
     * @Description: 用户分析列表
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018-08-08 16:12
     */
    @RequestMapping(value = "list")
    public ModelAndView list(Page page, StatisticsUserVisit suv, CommonInput commonInput) {
        page.setLimit(10);
        ModelAndView mv = new ModelAndView("analysis/statisticalUserList");
        if (commonInput.getTimeType() == null&&
                (StringUtils.isBlank(commonInput.getCreateEnd())||StringUtils.isBlank(commonInput.getCreateStart()))
                ){
            commonInput.setTimeType(2);
        }
        CommonInput.transfromDate(commonInput);
        Map<String, Object> params = Maps.newHashMap();
        params.put("startDate", commonInput.getCreateStart());
        params.put("endDate", commonInput.getCreateEnd());

        if (StringUtils.isNotEmpty(commonInput.getCreateStart())) {
            commonInput.setCreateStart(commonInput.getCreateStart().substring(0, "2018-05-04".length()));
        }
        if (StringUtils.isNotEmpty(commonInput.getCreateEnd())) {
            commonInput.setCreateEnd(commonInput.getCreateEnd().substring(0, "2018-05-04".length()));
        }
        List<StatisticsUserVisit> list = statisticsUserVisitService.webListPage(params, page);
        Long newNum = 0L, oldNum = 0L;
        for (StatisticsUserVisit statisticsUserVisit : list) {
            newNum += statisticsUserVisit.getNewNum();
            oldNum += statisticsUserVisit.getOldNum();
        }
        List<Map<String, Object>> userDatas = new LinkedList<>();
        Map<String, Object> newUserMap = Maps.newHashMap();
        newUserMap.put("name", "新用户");
        newUserMap.put("value", newNum);

        Map<String, Object> oldUserMap = Maps.newHashMap();
        oldUserMap.put("name", "老用户");
        oldUserMap.put("value", oldNum);
        userDatas.add(newUserMap);
        userDatas.add(oldUserMap);
        mv.addObject("userDatas", JSONObject.toJSONString(userDatas));
        mv.addObject("list", list);
        mv.addObject("page", page);
        mv.addObject("suv", suv);
        mv.addObject("input", commonInput);
        return mv;
    }

    /**
     * @Description: 用户分析单列
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018-08-08 16:12
     */
    @RequestMapping(value = "rowView")
    public AjaxResult rowView(String staDate, String endDate, Integer cycle) {
        AjaxResult ajaxResult = new AjaxResult();
        HashMap<String, Object> map = Maps.newHashMap();
        StatisticsUserVisit statisticsUserVisit = null;
        boolean of = false;
        try {
            //设置默认
            if (StringUtils.isBlank(staDate) && StringUtils.isBlank(endDate)) {
                SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
                Calendar cdate = Calendar.getInstance();
                cdate.setTime(new Date());
                cdate.set(Calendar.DATE, cdate.get(Calendar.DATE) - cycle);

                staDate = dft.format(new Date());
                endDate = dft.format(cdate.getTime());
            }
            statisticsUserVisit = statisticsUserVisitService.queryByDate(staDate, endDate);
            of = true;
        } catch (Exception e) {
            of = false;
        }
        map.put("date", statisticsUserVisit);
        ajaxResult.setData(map);
        ajaxResult.setSuccess(of);
        return ajaxResult;
    }
}
